package com.junmoo.www.module.system.service;

import com.junmoo.www.module.system.dao.SysResourceRepository;
import com.junmoo.www.module.system.entity.SysResource;
import com.junmoo.www.module.system.entity.SysRole;
import com.junmoo.www.utils.BeanUtil;
import com.junmoo.www.utils.R;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class SysResourceService {
    // 1、先引入实体类管理器
    @PersistenceContext
    private EntityManager entityManager;
    private SysResourceRepository sysResourceRepository;
    public List<SysResource> findAll(){
        return sysResourceRepository.findAllByOrderByResourceSortAsc();
    }


    public Page<SysResource> page(SysResource search, Pageable pageable) {
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("resourceName", ExampleMatcher.GenericPropertyMatcher::contains)
                .withMatcher("resourceCode", ExampleMatcher.GenericPropertyMatcher::contains)
                .withMatcher("resourceLink", ExampleMatcher.GenericPropertyMatcher::contains)
                ;
        Example<SysResource> example = Example.of(search,matcher);
        return sysResourceRepository.findAll(example,pageable);
    }

    @Transactional
    public R create(SysResource entity) {
        SysResource repoEntoty = sysResourceRepository.findByResourceCode(entity.getResourceCode());
        if(repoEntoty!=null) return R.error("资源名已存在！！！");
        // 排序字段设置
        if(entity.getResourceSort() ==null || entity.getResourceSort() ==0){
            String sql ="select max(resource_sort) from sys_resource where resource_type = 'menu'";
            entity.setResourceType("menu");
            if(entity.getParentId()!=0){
                SysResource parent = sysResourceRepository.getOne(entity.getParentId());
                if(parent.getResourceType().equalsIgnoreCase("menu")){
                    sql ="select max(resource_sort) from sys_resource where resource_type = 'page'";
                    entity.setResourceType("page");
                }else {
                    sql ="select max(resource_sort) from sys_resource where resource_type = 'button'";
                    entity.setResourceType("button");
                }
            }
            Query query = entityManager.createNativeQuery(sql);
            int sort= Integer.valueOf( String.valueOf(query.getSingleResult()) ) +1;
            entity.setResourceSort(sort);
        }

        SysResource returnEntity=sysResourceRepository.save(entity);
        if(returnEntity==null) return R.error("新建资源失败！！！");
        return R.success("success");
    }
    @Transactional
    public R edit(SysResource entity) {
        entity = BeanUtil.copyNullProperties(entity,sysResourceRepository.getOne(entity.getResourceId()));
        SysResource repoEntoty=sysResourceRepository.save(entity);
        if(repoEntoty==null) return R.error("资源不存在！！！");
        return R.success("success");
    }
    @Transactional
    public R deleteByIds(List<Number> ids) {
        ids.removeAll(Collections.singleton(null));
        if(CollectionUtils.isEmpty(ids)) return R.error("error");
        List<Long> list=ids.stream().map(id-> Long.valueOf(String.valueOf(id ))).collect(Collectors.toList());
        sysResourceRepository.deleteAll( sysResourceRepository.findAllById(list) );
        return R.success("success");
    }

}
